package com.Wlongtao.eBusiness.cotroller;

import com.Wlongtao.eBusiness.dao.PlayerRepository;
import com.Wlongtao.eBusiness.entity.Player;
import com.Wlongtao.eBusiness.services.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/players")
public class PlayerController {

    @Autowired
    PlayerService playerService;

    @GetMapping
    public String displayPlayers(Model model){

        List<Player> players = playerService.getAll();
        model.addAttribute("players",players);
        return "players/list-players";
    }

    @GetMapping("/new")
    public String displayPlayerForm(Model model){

        Player aPlayer = new Player();
        model.addAttribute("player",aPlayer);
        return "players/new-player";
    }

    @PostMapping("/save")
    public String createPlayer(Model model,Player player){

        playerService.save(player);

        return "redirect:/players";
    }


}
